define(function(){
	var windowDrag={
		$dragDom:null,
		$dragAreaDom:null,
		dragMode:'',
		mousedown:{
			x:-1,
			y:-1,
			left:-1,
			top:-1
		},
		mousemove:{
			x:0,
			y:0
		},
		dragStart:function($dragDom,e){
			windowDrag.dragMode='drag';
			windowDrag.$dragDom=$dragDom;
			windowDrag.mousedown={
				x:e.screenX,
				y:e.screenY,
				left:Number(windowDrag.$dragDom.css('margin-left').replace('px',''))||0,
				top:Number(windowDrag.$dragDom.css('margin-top').replace('px',''))||0
			};
		},
		dragMove:function(){
			if(windowDrag.$dragDom){
				var moveDistance={
					left:windowDrag.mousedown.x-windowDrag.mousemove.x,
					top:windowDrag.mousedown.y-windowDrag.mousemove.y
				}
				var dragTargetRect=windowDrag.$dragDom[0].getBoundingClientRect();
				var targetParentRect=windowDrag.$dragDom.parent()[0].getBoundingClientRect();
				/*if(moveDistance.left>=0){
					//向左
					if(dragTargetRect.right<targetParentRect.right){
						moveDistance.left=dragTargetRect.right-targetParentRect.right
					}else{
						windowDrag.$dragDom.css({
							'margin-left':windowDrag.mousedown.left-moveDistance.left
						});
					}

				}else{
					//向右
					if(dragTargetRect.left>targetParentRect.left){
						moveDistance.left=dragTargetRect.left-targetParentRect.left;
					}else{
						windowDrag.$dragDom.css({
							'margin-left':windowDrag.mousedown.left-moveDistance.left
						});
					}
				}*/
				/*windowDrag.$dragDom.css({
					'margin-left':windowDrag.mousedown.left-moveDistance.left
				});*/

				/*if(moveDistance.top>=0){
					//向上
					if(dragTargetRect.bottom<targetParentRect.bottom){
						moveDistance.top=dragTargetRect.bottom-targetParentRect.bottom
					}else{
						windowDrag.$dragDom.css({
							'margin-top':windowDrag.mousedown.top-moveDistance.top
						});
					}
				}else{
					//向下
					if(dragTargetRect.top>targetParentRect.top){
						moveDistance.top=dragTargetRect.top-targetParentRect.top;
					}else{
						windowDrag.$dragDom.css({
							'margin-top':windowDrag.mousedown.top-moveDistance.top
						});
					}
				}*/

				windowDrag.$dragDom.css({
					'margin-left':windowDrag.mousedown.left-moveDistance.left,
					'margin-top':windowDrag.mousedown.top-moveDistance.top
				});
			}
		},
		move:function(x,y){
			windowDrag.mousemove.x=x;
			windowDrag.mousemove.y=y;
			switch(windowDrag.dragMode){
				case 'drag':
					windowDrag.dragMove()
					break;

			}
		},
		moveStop:function(){
			windowDrag.$dragDom=null;
		}
		/*resize:function(){
			if(windowDrag.$dragDom){
				var myDistance={
					x:windowDrag.mousedown.x<0?0:windowDrag.mousedown.x-windowDrag.mousemove.x,
					y:windowDrag.mousedown.y<0?0:windowDrag.mousedown.y-windowDrag.mousemove.y
				}
				windowDrag.$dragDom.css('width',function(index,value){
					var myDistanceWidth=windowDrag.mousedown.width-myDistance.x;
					if(myDistanceWidth<windowDrag.mousedown.width_min()){
						myDistanceWidth=windowDrag.mousedown.width_min()
					}
					if(myDistanceWidth>windowDrag.mousedown.width_max()){
						myDistanceWidth=windowDrag.mousedown.width_max()
					}
					return myDistanceWidth
				})
				windowDrag.$dragDom.css('height',function(index,value){
					var myDistanceHeight=windowDrag.mousedown.height-myDistance.y
					if(myDistanceHeight<windowDrag.mousedown.height_min()){
						myDistanceHeight=windowDrag.mousedown.height_min()
					}
					if(myDistanceHeight>windowDrag.mousedown.height_max()){
						myDistanceHeight=windowDrag.mousedown.height_max()
					}
					return myDistanceHeight
				})
			}
			//return myDistance;
		},
		resizeStart:function($dragDom,e,minsize){
			windowDrag.dragMode='resize';
			if(minsize==undefined){
				minsize={
					width:480,
					height:320
				}
			}
			windowDrag.$dragDom=$dragDom;
			windowDrag.mousedown={
				x:e.screenX,
				y:e.screenY,
				width:windowDrag.$dragDom.outerWidth(),
				height:windowDrag.$dragDom.outerHeight(),
				width_min:function(){
					return minsize.width;
				},
				height_min:function(){
					return minsize.height;
				},
				width_max:function(){
					return  $(window).width()-20;
				},
				height_max:function(){
					return $(window).height()-20;
				}
			}
		},*/
	};
	$('body').on('mousedown','[data-dragarea]',function(e){
		windowDrag.dragStart($(this).closest($(this).attr('data-dragarea')),e)
	});
	/*$('body').on('mousedown','[data-resizearea]',function(e){
		windowDrag.resizeStart($(this).closest($(this).attr('data-resizearea')),e)
	});*/

	// ie disabled select
	$('body').on('selectstart drag','[data-dragarea],[data-resizearea]',function(e){
		return false;
	});

	/*document.body.onselectstart = document.body.ondrag = function(){
		return false;
	}*/

	var $dragAreaDom=$(window);
	if(!+[1,]){
		$dragAreaDom=$('body');//ie is$('body');
	}
	/*if(!isIframe){
		$dragAreaDom=$('body');//ie is$('body');
	}*/
	$dragAreaDom.on('mousemove',function(e){
		if(windowDrag.$dragDom){
			windowDrag.move(e.screenX,e.screenY);
		}
	});
	$dragAreaDom.on('mouseup',function(e){
		if(windowDrag.$dragDom){
			windowDrag.moveStop();
		}
	});
	window.windowDrag=windowDrag;
	return windowDrag;
})
